package com.itheima.springbootwebtlias.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.springbootwebtlias.mapper.StudentMapper;
import com.itheima.springbootwebtlias.pojo.PageResult;
import com.itheima.springbootwebtlias.pojo.Student;
import com.itheima.springbootwebtlias.pojo.StudentQueryParam;
import com.itheima.springbootwebtlias.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult queryStudent(StudentQueryParam studentQueryParam) {

        //1.启动
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());

        Page<Student> page = (Page<Student>) studentMapper.selectbyCondition(studentQueryParam);

        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());

        return pageResult;
    }

    @Override
    public void addStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.addStudent(student);
    }

    @Override
    public Student selectByIdStudent(Integer id) {
        Student student = studentMapper.selectByIdStudent(id);
        return student;
    }

    @Override
    public void updataStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updataStudent(student);

    }

    @Override
    public void updataByIdScoreViolation(Integer id, Integer score) {
//        Student student = new Student();
//        student.setViolationCount();
        studentMapper.updataByIdScoreViolation(id,score);
    }

    @Override
    public void deleteByIdStudent(List<Integer> ids) {
        studentMapper.deleteByIdStudent(ids);

    }


}
